from flask import Flask,request
from flask_pymongo import PyMongo
import re
import jieba
import os
import langconv

app = Flask(__name__)
host = "localhost"
port = "8000"
app.config["MONGO_URI"] = "mongodb://"+host+":27017/eye-trace"
mongo = PyMongo(app)
stop_word_dir = 'stopwords'

# 创建停用词列表
def stopwordslist():
    stopwords = [line.strip() for line in open(os.path.join(stop_word_dir,'百度停用词表.txt'),encoding='UTF-8').readlines()]
    stopwords.append(" ")
    return stopwords
stopwords = stopwordslist()
@app.route('/')
def hello_world():
    print(stopwords)
    return 'Hello World!'

 # 将sentence中的繁体字转为简体字
def Traditional2Simplified(sentence):
    sentence = langconv.Converter('zh-hans').convert(sentence)
    return sentence

# 清理一下评论数据
def clear(string):
    #     string = re.sub('[\s]','',string)  # 去掉空格等空白符号
    string = re.sub('[a-zA-Z]', '', string)  # 去掉英文字母
    #     string = re.sub(r"[！!？｡。，&;＂★＃＄％＆＇（）＊＋－／：；＜＝＞＠［＼］＾＿｀｛｜｝～｟｠｢｣､、〃「」『』【】〔〕〖〗〘〙#〚〛〜〝〞/?=~〟,〰–—‘’‛“”„‟…‧﹏.]", "", string)  # 去掉中文符号
    #     string = re.sub(r"[! \'\"#。$%&()*+,-.←→/:~;:<=>?@[\\]^_`_{|}~()]","", string)  # 去掉英文符号
    punctuation_string = '!"#$%&\'\"()。*+,-.←→/:~;:<=>?@[\\]^_`_{|}~'
    string = re.sub('[{}]'.format(punctuation_string), " ", string)  # 去掉英文符号

    punctuation_str = '＂＃＄％＆＇（）＊＋，－／：；＜＝＞＠［＼］＾＿｀｛｜｝～｟｠｢｣､\u3000、〃〈〉《》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏﹑﹔·！？｡。'
    string = re.sub('[{}]'.format(punctuation_str), " ", string)  # 去掉中文符号

    #     string = string.replace("\n"," ,")
    string = ' '.join(string.split())
    return Traditional2Simplified(string).lower()  # 所有的英文都换成小写

def update_keywords(keywords,word_list):
    for word in word_list:
        keywords[word]=keywords.get(word,0)+1
    return keywords

def cut_words(word_string):
    word_list = jieba.lcut(word_string)
    return word_list

def update_user(user_id,text):
    # 去除标点，繁体转简体
    text_cleared = clear(text)
    word_list = cut_words(text_cleared)
    # 去除停用词
    word_list_trimd = []
    for word in word_list:
        if word not in stopwords and word.__len__()>1:
            word_list_trimd.append(word)
    user_data = mongo.db.user.find_one({"_id":user_id})
    if user_data != None:
        keywords = update_keywords(keywords=user_data['keywords'],word_list=word_list_trimd)
        query = {"_id": user_id}
        update = {"$set": {"keywords": keywords}}
        response = mongo.db.user.update_one(query,update)
        return response
    else:
        keywords = update_keywords(keywords={}, word_list=word_list_trimd)
        save = {"_id":user_id,"keywords":keywords}
        response = mongo.db.user.insert_one(save)
        return response
def update_last_user(user_id):
    query = {"_id": "last_user"}
    update = {"$set": {"last_user_id": user_id}}
    response = mongo.db.temp.update_one(query, update)

@app.route("/update_user")
def update_user_api():
    try:
        user_id = request.args.to_dict().get("user_id")
        text = request.args.to_dict().get("text")
        response = update_user(user_id,text)
        update_last_user(user_id)
        user_data = mongo.db.user.find_one(user_id)
        return user_data
    except Exception as e:
        return str(e)

if __name__ == '__main__':
    app.run(host='0.0.0.0',port=port)
